/*
 * =====================================================================================
 *
 *       Filename:  config.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  23.08.2012 10:49:50
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  akoww (), 
 *   Organization:  
 *
 * =====================================================================================
 */

#include "config.hpp"

namespace Drosera {

Configuration Config::m_config;


bool Config::readConfig ( const char *path ) {

	std::fstream pfile ( path, std::ios::in | std::ios::binary );
	
	if ( !m_config.ParseFromIstream ( &pfile )) {
     		warning ( "Can't find or parse config-file." );
		return false;
      	}
	return true;

}

bool Config::checkExist ( const char* path ) {
	
	std::fstream pfile ( path, std::ios::in | std::ios::binary );
	Drosera::Configuration conf;

	if ( !conf.ParseFromIstream ( &pfile ))
		return false;
	return true;
}

bool Config::createConfig ( config_t &conf ) {

	std::fstream pfile ( conf.path.c_str(), std::ios::out | std::ios::trunc | std::ios::binary  );

	m_config.set_port ( conf.port );
	m_config.set_host ( conf.host );
	m_config.set_certprv ( conf.prv );
	m_config.set_certpub ( conf.pub );

	if ( !m_config.SerializeToOstream ( &pfile ) ) {
		error ( "Cant write to config-file." );
		return false;
	}
	info ( "Configuration generated!" );
	return true;
}

unsigned int Config::getPort ( void ) {
	return m_config.port();
}

const std::string& Config::getHost ( void ) {
	return m_config.host();
}

const std::string& Config::getCertPrv ( void ) {
	return m_config.certprv();
}

const std::string& Config::getCertPub ( void ) {
	return m_config.certpub();
}


Config::Config ( void ) {

}

Config::~Config ( void ) {

}

}

